/*
 * @lc app=leetcode.cn id=297 lang=cpp
 *
 * [297] 二叉树的序列化与反序列化
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include <iostream>
#include <list>
#include <string>

class Solute
{
 public:
  // Encodes a tree to a single string.
  std::list<std::string> split(std::string& str, char c)
  {
    std::list<std::string> res;
    std::list<std::string>::iterator it = res.begin();
    for (int lastpos = -1, pos = 0; pos < str.length(); pos++)
    {
      if (str[pos] == c)
      {
        // std::string a = str.substr(++lastpos, pos - lastpos);
        // std::cout << a << std::endl;
        // res.push_back(1,)
        // res.push_back(2,)
        // res.push_back(X,)
        // 字符串中，分隔符是默认的
        res.push_back(str.substr(++lastpos, pos - lastpos));
        std::cout << a << std::endl;
        for (; it != res.end(); ++it)
        {
          std::cout << *it << std::endl;
        }
        lastpos = pos;
      }
    }
    return res;
  }
};

int main()
{
  class Solute cod;
  std::string data = "1,2,X,3,4";
  std::list<std::string> result = cod.split(data, ',');
  // std::cout << result << '\n';

  return 0;
}

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
// @lc code=end
